package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.attr.entity.BootstrapMethodsEntity;
import com.mazaiting.classfile.constant.ConstantPool;

import java.util.Arrays;

/**
 * 启动方法属性
 *
 * @author mazaiting
 * @since 2021/8/13
 */
public class BootstrapMethodsAttribute extends AttributeInfo{
    /**
     * 启动方法数量 u2
     */
    private int numBootstrapMethods;
    /**
     * 启动方法参数实体
     */
    private BootstrapMethodsEntity[] bootstrapMethodsEntities;

    public BootstrapMethodsAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.numBootstrapMethods = reader.readU2ToUnsignedShort();
        this.bootstrapMethodsEntities = new BootstrapMethodsEntity[numBootstrapMethods];
        for (int i = 0; i < numBootstrapMethods; i++) {
            this.bootstrapMethodsEntities[i] = new BootstrapMethodsEntity(
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2UnsignedShorts()
            );
        }
    }

    @Override
    public String toString() {
        return "BootstrapMethodsAttribute{" +
                "nameIndex=" + nameIndex +
                ", length=" + length +
                ", numBootstrapMethods=" + numBootstrapMethods +
                ", bootstrapMethodsEntities=" + Arrays.toString(bootstrapMethodsEntities) +
                '}';
    }
}


















